package com.xue.gateway;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.web.server.ServerWebExchange;

import reactor.core.publisher.Mono;

/**
 * 
 * @ClassName: HostAddrKeyResolver 
 * @Description: 自定义网关限流维度
 * @author zzx
 *
 * @date 2020年1月3日
 */
public class HostAddrKeyResolver implements KeyResolver{
	
	@Override
	public Mono<String> resolve(ServerWebExchange exchange) {
		//根据Hostname进行限流，则需要用hostAddress去判断
		//return Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
		//根据uri去限流
		//return Mono.just(exchange.getRequest().getURI().getPath());
		//用户的维度去限流 请求参数必须包含 userId
		return  Mono.just(exchange.getRequest().getQueryParams().getFirst("userId"));
	}
}
